import { useRoom } from "@/stores/room"
import { useServers } from "@/stores/servers"
import type { element } from "../element"
import type cardType from "../cardType/cardType";
export enum RequestType {
    GET_INIT_DATA = 'get_init_data',
    POST_INIT_DATA = 'post_init_data',
    POST_USER_ROLE = 'post_user_role',
    SELF_AUTHOR = 'self_author',
    POST_ELEMENT_DATA = 'post_element_data',
    POST_CARDTYPE_DATA = 'post_cardtype_data',
    POST_ROOM_ROLE = 'post_room_role',
    
    //never use
    RECOGNIZE_MESSAGE = 'recognize_message',
}
export default interface requestMessage {
    body: string
    type: RequestType;
    receiver: string
    msgId?: string
}
export function reqToJson(req: requestMessage) {
    return JSON.stringify(req)
}
export function reqPostInitData(recevier: string): requestMessage {
    //TODO 权限控制
    const room = useRoom()
    return {
        body:JSON.stringify(room.usingRoom),
        type: RequestType.POST_INIT_DATA,
        receiver: recevier,
    }
}
export function reqGetInitData(): requestMessage {
    return {
        body: '',
        type: RequestType.GET_INIT_DATA,
        receiver: ''
    }
}
export function reqPostUserRole(target: string, role: string[]): requestMessage {
    return {
        type: RequestType.POST_USER_ROLE,
        receiver: '*',
        body: JSON.stringify({
            target: target,
            role: role
        })
    }
}
export function reqPostRoomRole(roll :string[]): requestMessage {
    return {
        type: RequestType.POST_ROOM_ROLE,
        receiver: '*',
        body: JSON.stringify(roll)
    }

}
export function reqSelfAuthor(): requestMessage {
    const servers = useServers()
    return {
        type: RequestType.SELF_AUTHOR,
        receiver: '',
        body: servers.connectSetting.password
    }
}
export function reqPostElementData(elementId: string, recevier: string, element: element|undefined): requestMessage {
    return {
        type: RequestType.POST_ELEMENT_DATA,
        receiver: recevier,
        body: JSON.stringify({
            id: elementId,
            data: element
        })
    }
}
export function reqPostCardTypeData(cardTypeName: string, recevier: string, cardType: cardType|undefined): requestMessage {
    return {
        type: RequestType.POST_CARDTYPE_DATA,
        receiver: recevier,
        body: JSON.stringify({
            id: cardTypeName,
            data: cardType
        })
    }
}